<?php
$seedWord = !isset($_GET['kw']) ? 'ipad2' : trim($_GET['kw']);
$limit = !isset($_GET['limit']) ? 30 : intval($_GET['limit']);
$matchType = !isset($_GET['mt']) ? 3 : intval($_GET['mt']);
$ot = !isset($_GET['ot']) ? 'html' : $_GET['ot'];

$cache_file = dirname(__FILE__).'/tmp/'.md5("$seedWord&$limit&$matchType").'.txt';
if (file_exists($cache_file) && (time() - filemtime($cache_file)) < 864000 ) {
    $kws = json_decode(file_get_contents($cache_file));
    $header['status'] = 'cached';
    $header['count'] = count($kws);
    $header['remain'] = 'n/a';
} else {
    require_once 'soapclientcore.php';
    class KRService extends Baidu_Api_Client_Core {
        public function __construct() {
            parent::__construct('KRService');
        }
    }
    $service = new KRService();
    $output_headers = array();
    $arguments = array('getKRbySeedWordRequest' => array('seedWord' => $seedWord, 'seedFilter' => array('matchType'=>$matchType, 'maxNum'=>$limit)));
    $output_response = $service->soapCall('getKRbySeedWord', $arguments, $output_headers);
    $kws = array();
    foreach($output_response->krResult as $it) {
        $kws[$it->word] = $it->broadPV;
    }
    arsort($kws);
    $header['status'] = $output_headers['ResHeader']->desc;
    $header['count'] = $output_headers['ResHeader']->oprs;
    $header['remain'] = $output_headers['ResHeader']->rquota;
    if ($header['status']=='success' && count($kws)>1) {
        file_put_contents($cache_file, json_encode($kws)); 
    }
}

if ($ot=='html') {
    header('Content-Type: text/html; charset=utf-8;');
    echo "<form action='' method='get'><input type='text' name='kw' value='".htmlspecialchars($seedWord)."' /> <input type='submit' value='GO' /> <input type='hidden' value='30' name='limit' /></form> <a href='?kw=".urlencode($seedWord)."&limit=$limit&mt=$matchType&ot=xml' target='_blank' />XML</a> <a href='?kw=".urlencode($seedWord)."&limit=$limit&mt=$matchType&ot=json' target='_blank' />JSON</a><table>";
    foreach ($kws as $k=>$v) {
        echo "<tr><td>{$k}</td><td>{$v}</td></tr>\n";
    }
    echo "</table>";
    echo "<br /><i>Status: {$header['status']}</i>";
    echo "<br /><i>Count: {$header['count']}</i>";
    echo "<br /><i>Remain: {$header['remain']}</i>";
    die();
}
if ($ot=='xml') {
    header('Content-Type: text/xml; charset=utf-8;');
    echo '<?xml version="1.0" encoding="utf-8"?><baidu><request><seedWord><![CDATA['.$seedWord.']]></seedWord><matchType>'.$matchType.'</matchType><limit>'.$limit.'</limit></request>';
    echo "<response><header><status><![CDATA[{$header['status']}]]><count>{$header['count']}</count><remain>{$header['remain']}</remain></status></header>";
    foreach ($kws as $k=>$v) {
        echo "<item><kw>{$k}</kw><pv>{$v}</pv></item>\n";
    }
    echo "</response></baidu>";
    die();
}
if ($ot=='json') {
    header('Content-Type: application/json; charset=utf-8;');
    $header['seedWord'] = $seedWord;
    $header['matchType'] = $mt;
    $header['limit'] = $limit;
    die(json_encode(array('header'=>$header, 'body'=>$kws)));
}
